﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using HISModels.Entities;
using System.IO;
using Newtonsoft.Json;
using System.Configuration;
using Newtonsoft.Json.Linq;

namespace HIS.Controllers.Home {

    public class PopUp : HIS.Controllers.AppBaseController {

        public PopUp() {
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public override void Index() {
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void Doctors() {
            try {
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpSearchDoctor() {
            try {
                string ddlSpecialist = Utils.Helper.DropDownSpecialist("TextBoxDoctorSpecialist", "",false, base.RestService);
                Data.Add("ddlSpecialists", ddlSpecialist);
                RenderFile("FrontOffice\\registrationsearchdoctor.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListDoctor() {
            string strJson = string.Empty;
            string sEcho = HttpContext.Current.Request["sEcho"];
            try {
                string doctorname = HttpContext.Current.Request["doctorname"];
                string code = HttpContext.Current.Request["code"];
                string doctorspecialist = HttpContext.Current.Request["TextBoxDoctorSpecialist"];

                string ssearch = "doctorname=" + doctorname + ";code=" + code + ";doctorspecialist=" + doctorspecialist;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["doctorschedulepaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();

                ///Convert to JObject
                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["aaData"];

                ///Modify it
                JArray aaDataModified = new JArray();

                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    oo["doctorenkrp"] = KeanuMvc.Reference.CryptoString.Encrypt((oo["Id"] + ";" + oo["DoctorName"] + ";" + oo["UserId"]).ToString());
                    aaDataModified.Add(oo);
                }
                o["aaData"] = aaDataModified;
                strJson = JsonConvert.SerializeObject(o);

                stringReader.Dispose();
                base.RestService.Dispose();

            }  catch (Exception ex) {
                strJson = "{\"sEcho\":\"" + sEcho + "\",\"iTotalRecords\":0,\"iTotalDisplayRecords\":0,\"ExceptionMessage\":\"Exception Occured : "+ex.Message+"\",\"aaData\":[]}";
            } finally {
                HttpContext.Current.Response.Write(strJson);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpSearchAllDoctor() {
            try {
                string ddlSpecialist = Utils.Helper.DropDownSpecialist("TextBoxDoctorSpecialist", "",false, base.RestService);
                Data.Add("ddlSpecialists", ddlSpecialist);
                RenderFile("FrontOffice\\registrationsearchalldoctor.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListAllDoctor() {
            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string doctorname = HttpContext.Current.Request["doctorname"];
                string code = HttpContext.Current.Request["code"];
                string doctorspecialist = HttpContext.Current.Request["TextBoxDoctorSpecialist"];

                string ssearch = "doctorspecialist=" + doctorspecialist + ";doctorname=" + doctorname + ";code=" + code;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["doctorpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                strJson = stringReader.ReadToEnd();

                //Dummy
                //strJson = "{\"sEcho\":1,\"iTotalRecords\":1,\"iTotalDisplayRecords\":1,\"aaData\":[{\"Id\":9,\"CodeValue\":\"10100100009\",\"FirstName\":\"Gunawan\",\"LastName\":\"Darmansyah\",\"DoctorName\":\"Gunawan Darmansyah\",\"StartTime\":\"00:00:00\",\"EndTime\":\"00:00:00\",\"UserId\":\"physician-doctor\",\"DoctorSpecialist\":\"Internist\"}]}";
                stringReader.Dispose();
                base.RestService.Dispose();

                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpSearchExtDoctor() {
            try {
                string ddlSpecialist = Utils.Helper.GetDocSpecialistVal("TextBoxDoctorSpecialist", "",string.Empty, false, base.RestService);
                Data.Add("ddlSpecialists", ddlSpecialist);
                RenderFile("FrontOffice\\registrationsearchextdoctor.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListExtDoctor() {
            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string doctorname = HttpContext.Current.Request["doctorname"];
                string code = HttpContext.Current.Request["code"];
                string doctorspecialist = HttpContext.Current.Request["TextBoxDoctorSpecialist"];

                string ssearch = "doctorspecialist=" + doctorspecialist + ";doctorname=" + doctorname + ";code=" + code;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["doctorextpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                strJson = stringReader.ReadToEnd();
                //strJson = "{\"sEcho\":2,\"iTotalRecords\":22,\"iTotalDisplayRecords\":22,\"aaData\":[]}";
                
                stringReader.Dispose();
                base.RestService.Dispose();

                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }
        
        
        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpSearchArea() {
            try {
                Address oPresentAddress = new Address(); Utils.Helper.SetNullToEmptyString(oPresentAddress);
                oPresentAddress.CountryId = 100;// 100 ->indonesia 
                Data.Add("DdPresentCountry", Utils.Helper.GetCountry("PresentAddressCountryId", oPresentAddress.CountryId.ToString()).NullSafeToString());
                RenderFile("patient\\registrationsearcharea.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }


        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListArea() {
            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string DialogTextBoxArea = HttpContext.Current.Request["DialogTextBoxArea"].NullSafeToString();
                string DialogTextBoxCity = HttpContext.Current.Request["DialogTextBoxCity"].NullSafeToString();
                string DialogTextBoxProvince = HttpContext.Current.Request["DialogTextBoxProvince"].NullSafeToString();
                string PresentAddressCountryId = HttpContext.Current.Request["PresentAddressCountryId"].NullSafeToString();
                string ssearch = string.Empty;
                ssearch = "area=" + DialogTextBoxArea + ";city=" + DialogTextBoxCity + ";province=" + DialogTextBoxProvince + ";country=" + PresentAddressCountryId;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["areapaging"] + Global.ServiceUriDictionary["requestformat"];
                //string strUri =    Data["servicerooturl"] + "opd/area/paging/?format=json";
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();


                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpPassportDetail() {
            try {
                Address oAddress = new Address(); Utils.Helper.SetNullToEmptyString(oAddress);
                oAddress.CountryId = 100;// 100 ->indonesia
                Data.Add("DdNationality", Utils.Helper.GetCountry("DdPassportNationality", oAddress.CountryId.ToString()).NullSafeToString());
                RenderFile("patient\\registrationpassportdetail.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PopUpSearchPayer() {
            try {
                Data.Add("ddlPayerCategory", Utils.Helper.GetPatientPayer("ddlPayerCategory", string.Empty, string.Empty).NullSafeToString());
                RenderFile("patient\\patientsearchpayer.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListPayer() {
            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string ddlPayerCategory = HttpContext.Current.Request["ddlPayerCategory"];
                if (string.IsNullOrEmpty(ddlPayerCategory)) {
                    ddlPayerCategory = "0";
                }
                string TextBoxPayerName = HttpContext.Current.Request["TextBoxPayerName"];
                string TextBoxPayerCode = HttpContext.Current.Request["TextBoxPayerCode"];

                string ssearch = "ddlPayerCategory=" + ddlPayerCategory + ";TextBoxPayerName=" + TextBoxPayerName + ";TextBoxPayerCode=" + TextBoxPayerCode;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["payerpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();


                JObject Jobj = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)Jobj["aaData"];
                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    string PhoneNumber = (string)oo["Phone"];
                    string Fax = (string)oo["Fax"];
                    string AddressDesc = (string)oo["AddressDesc"];
                    if (string.IsNullOrEmpty(PhoneNumber)) {
                        oo["Phone"] = string.Empty;
                    }
                    if (string.IsNullOrEmpty(Fax)) {
                        oo["Fax"] = string.Empty;
                    }
                    if (string.IsNullOrEmpty(AddressDesc)) {
                        oo["AddressDesc"] = string.Empty;
                    }
                    aaDataModified.Add(oo);
                }
                Jobj["aaData"] = aaDataModified;
                strJson = JsonConvert.SerializeObject(Jobj);
                stringReader.Dispose();
                base.RestService.Dispose();

                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }
    }

}